package com.lgkj.ui;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.GameAdapter;
import com.lgkj.bean.GameBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/21 0021.
 */

public class PlayGameActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.lv_game)
    ListView lvGame;
    private GameAdapter adapter;
    private GameBean gameBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_play_game;
    }

    @Override
    public void initView() {
        setTitle("我爱小游戏");
        adapter = new GameAdapter(PlayGameActivity.this);
        lvGame.setAdapter(adapter);
        lvGame.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        showDiaLog("正在获取");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(PlayGameActivity.this));
        HttpUtils.postHttp(map, CommonConstant.gamelist, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                Gson mGson = new Gson();
                if (jsonObject.optInt("code") == 0) {
                    gameBean = mGson.fromJson(String.valueOf(jsonObject), GameBean.class);
                    if (gameBean != null && gameBean.getData() != null) {
                        adapter.setData(gameBean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }

            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        GameBean.DataBean adapterItem = (GameBean.DataBean) adapter.getItem(i);
        String title = adapterItem.getGame_title();
        Intent intent = new Intent(PlayGameActivity.this, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", adapterItem.getGame_link());
        startsActivity(intent);

    }
}
